#include "Tetrimino.h"

Tetrimino::Tetrimino()
{
	type = 'X';
}

Tetrimino::Tetrimino(int t)
{
	Init(t);
}

bool Tetrimino::Init(int t)
{
	switch (t)
	{
	case O_BLOCK:
		setO();
		return true;
		break;
	case Z_BLOCK:
		setZ();
		return true;
		break;
	case S_BLOCK:
		setS();
		return true;
		break;
	case T_BLOCK:
		setT();
		return true;
		break;
	case L_BLOCK:
		setL();
		return true;
		break;
	case J_BLOCK:
		setJ();
		return true;
		break;
	case I_BLOCK:
		setI();
		return true;
		break;
	default:
		return false;
		break;
	}
	return false;
}

void Tetrimino::setO()
{
	type = 'O';
	color = RED;

	Points[0].X = 0; Points[0].Y = 0;
	Points[1].X = 1; Points[1].Y = 0;
	Points[2].X = 0; Points[2].Y = -1;
	Points[3].X = 1; Points[3].Y = -1;
}

void Tetrimino::setZ()
{
	type = 'Z';
	color = BLUE;

	Points[0].X = -1; Points[0].Y = 0;
	Points[1].X = 0; Points[1].Y = 0;
	Points[2].X = 0; Points[2].Y = 1;
	Points[3].X = 1; Points[3].Y = 1;

}

void Tetrimino::setS()
{
	type = 'S';
	color = GREEN;

	Points[0].X = -1; Points[0].Y = 1;
	Points[1].X = 0; Points[1].Y = 1;
	Points[2].X = 0; Points[2].Y = 0;
	Points[3].X = 1; Points[3].Y = 0;
}

void Tetrimino::setT()
{
	type = 'T';
	color = PURPLE;

	Points[0].X = -1; Points[0].Y = 0;
	Points[1].X = 0; Points[1].Y = 0;
	Points[2].X = 1; Points[2].Y = 0;
	Points[3].X = 0; Points[3].Y = -1;
}

void Tetrimino::setL()
{	
	type = 'L';
	color = YELLOW;

	Points[0].X = -1; Points[0].Y = 0;
	Points[1].X = 0; Points[1].Y = 0;
	Points[2].X = 1; Points[2].Y = 0;
	Points[3].X = 1; Points[3].Y = 1;
}

void Tetrimino::setJ()
{
	type = 'J';
	color = ORANGE;

	Points[0].X = -1; Points[0].Y = 0;
	Points[1].X = 0; Points[1].Y = 0;
	Points[2].X = 1; Points[2].Y = 0;
	Points[3].X = 1; Points[3].Y = -1;
}

void Tetrimino::setI()
{
	type = 'I';
	color = WHITE;

	Points[0].X = -1; Points[0].Y = 0;
	Points[1].X = 0; Points[1].Y = 0;
	Points[2].X = 1; Points[2].Y = 0;
	Points[3].X = 2; Points[3].Y = 0;
}

void Tetrimino::RotateL()
{
	if(type == 'O')
		return;

	COORD T[P_NUM];

	for(int x = 0; x < P_NUM; x++)
	{
		T[x].X = Points[x].X;
		T[x].Y = Points[x].Y;
	}

	int M[2][2] = { {0, 1}, {-1, 0} };

	for(int x = 0; x < P_NUM; x++)
	{
		Points[x].X = ((T[x].X*M[0][0])+(T[x].Y*M[1][0]));
		Points[x].Y = ((T[x].X*M[0][1])+(T[x].Y*M[1][1]));
	}
}

void Tetrimino::RotateR()
{
	if(type == 'O')
		return;

	COORD T[P_NUM];

	for(int x = 0; x < P_NUM; x++)
	{
		T[x].X = Points[x].X;
		T[x].Y = Points[x].Y;
	}

	int M[2][2] = { {0, -1}, {1, 0} };

	for(int x = 0; x < P_NUM; x++)
	{
		Points[x].X = ((T[x].X*M[0][0])+(T[x].Y*M[1][0]));
		Points[x].Y = ((T[x].X*M[0][1])+(T[x].Y*M[1][1]));
	}
}

void Tetrimino::Draw(DxManager * dxm,int px,int py)
{
	T_Block drawBlock;
	drawBlock.Init(dxm);
	for(int i = 0; i < P_NUM; i++)
	{
		drawBlock.Draw(dxm,px+Points[i].X,py+Points[i].Y, getColor());
	}
}

Tetrimino Tetrimino::operator +(Tetrimino *right)
{
	this->type = right->type;
	this->color = right->color;
	for(int i = 0; i < P_NUM; ++i)
	{
		this->Points[i].X = right->Points[i].X;
		this->Points[i].Y = right->Points[i].Y;
	}
	return *this;
}

bool Tetrimino::isNull()
{
	if(type == 'X')
		return true;
	else
		return false;
}